package com.example.java_second_hand_market;

import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
import javafx.scene.control.TextArea;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Modality;
import javafx.stage.Stage;

import java.sql.*;
import java.util.Vector;

public class ChatDialog extends Stage {
    BorderPane bp;
@FXML
    Label receivelabel =new Label("发给谁：");
    @FXML
    Label receivernamelabel =new Label();
    @FXML
    ComboBox<String> cb=new ComboBox<>();
    @FXML
    Label tL1=new Label();
    @FXML
    Label tL2=new Label();
    @FXML
    Label tL3=new Label();
    @FXML
    Label tL4=new Label();
    @FXML
    Label tL5=new Label();
    @FXML
    Label tL6=new Label();
    @FXML
    Label tL7=new Label();
    @FXML
    TextArea ta=new TextArea();
    @FXML
    Button submit=new Button("发送");

    Vector<Label> vl=new Vector<>();
    User sender,receiver;
    String DBUrl1=ParameterDB.ParaDBUserUrl;

    String DBUser="root", DBPassword="123456";
    Connection con;


    public ChatDialog(User us,int width,int height){

        sender=us;
        vl.addElement(tL1);
        vl.addElement(tL2);
        vl.addElement(tL3);
        vl.addElement(tL4);
        vl.addElement(tL5);
        vl.addElement(tL6);;
        vl.addElement(tL7);
        try{
            con=DriverManager.getConnection(DBUrl1,DBUser,DBPassword);

            Statement stmt1=con.createStatement();
            ResultSet rs1=stmt1.executeQuery("SELECT *  FROM user_list");
            while(rs1.next()){
                cb.getItems().add(rs1.getString("name"));
            }



            if(con!=null) {
                try {
                    rs1.close();


                    stmt1.close();

                    con.close();

                } catch (SQLException err) {
                    System.out.println("1");
                }
            }
        }catch (SQLException e){
            CommonDialog.WarningDialog("数据库装载失败！");
        }
        cb.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent actionEvent) {
                for (int i = 0; i < 7; i++) {
                    vl.elementAt(i).setText("");
                }
                cbaction();
            }
        });
        submit.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent actionEvent) {
                submitaction();
            }
        });

        VBox vb1=new VBox();
        VBox vb2= new VBox();
        HBox hb1 = new HBox();
        hb1.getChildren().addAll(receivelabel, receivernamelabel,cb);
        hb1.setMargin(receivelabel, new Insets(3,0,0,0));
        hb1.setMargin(receivernamelabel, new Insets(0,0,0,3));
        hb1.setMargin(cb,new Insets(0,0,0,3));

        HBox hb2 = new HBox();
        hb2.getChildren().addAll(submit);
        ta.setMaxHeight(100);
        hb2.setMargin(submit, new Insets(0,0,0,3));

        vb2.getChildren().addAll(ta,hb2);
        vb1.getChildren().addAll(tL1,tL2,tL3,tL4,tL5,tL6,tL7);

        bp=new BorderPane();
        bp.setTop(hb1);
        bp.setCenter(vb1);
        bp.setBottom(vb2);

        setX(820);
        setY(500);
        initModality(Modality.APPLICATION_MODAL);
        setScene(new Scene(bp));

        setWidth(400);
        setHeight(300);
        setX((width-getWidth()) / 2);
        setY((height-getHeight())/ 2);
        setTitle("聊天");

    }
    public void cbaction(){
        try {
        Connection con1=DriverManager.getConnection(DBUrl1,DBUser,DBPassword);
        Statement stmt1=con1.createStatement();
        String selectedOption = cb.getSelectionModel().getSelectedItem();
        receivernamelabel.setText(selectedOption);

            ResultSet rs2 = stmt1.executeQuery("SELECT * FROM user_list WHERE name = '" + selectedOption + "'");
            rs2.next();
            receiver= new User(rs2.getString("name"),rs2.getString("account"),rs2.getString("password"));
            rs2=stmt1.executeQuery("SELECT * FROM chat_list WHERE (sender = '"+sender.getName()+"' AND receiver = '"+receiver.getName()+"') OR (sender = '"+receiver.getName()+"' AND receiver = '"+sender.getName()+"') ORDER BY id desc");
            int a=7;
            while(a!=0) {
                a--;

                if (rs2.next()) {


                    vl.elementAt(a).setText(rs2.getString("sender")+":"+rs2.getString("message"));
                } else {
                    break;
                }
            }
                if(con1!=null) {
                    try {

                        rs2.close();
                        stmt1.close();
                        con1.close();
                    } catch (SQLException err) {
                        System.out.println("123456");
                    }
                }

        }catch (SQLException s){
            System.out.println("错误操作！"+s.getSQLState()+s.getMessage()+" "+s.getErrorCode());
        }
    }

    public void submitaction(){
        String textword =(ta.getText());
        try{
            Connection con1=DriverManager.getConnection(DBUrl1,DBUser,DBPassword);
            Statement stmt=con1.createStatement();
            stmt.executeUpdate("INSERT INTO chat_list (sender,receiver,message)VALUE('"+sender.getName()+"','"+receiver.getName()+"','"+textword+"');");
            ResultSet rs=stmt.executeQuery("SELECT * FROM chat_list WHERE (sender = '"+sender.getName()+"' AND receiver = '"+receiver.getName()+"') OR (sender = '"+receiver.getName()+"' AND receiver = '"+sender.getName()+"') ORDER BY id desc");
            int a=7;
            while(a!=0){
                a--;

                if(rs.next()) {
                    vl.elementAt(a).setText(rs.getString("sender")+":"+rs.getString("message"));
                }else break;

            }
            if(con1!=null) {
                try {
                    rs.close();
                    stmt.close();
                    con1.close();

                } catch (SQLException err) {
                    System.out.println("111111");
                }
            }
        }catch(SQLException e){
            CommonDialog.WarningDialog("数据库装载失败");
        }
        ta.clear();
    }
    public BorderPane getBp() {
        return bp;
    }

}
